<template>
	<view class="view">
		<view class="list">
			<view class="item" v-for="(item,index) in list" :key="index">
				<view class="info">
					<view>兑换 | {{item.productName || ''}}</view>
					<view>{{item.createTime}}<text>{{item.orderStatus | formatStatus}}</text></view>
				</view>
				<view class="integral">
					<view>-{{item.amount}}积分</view>
					<view>剩余 {{item.balance}} 积分</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				userId: uni.getStorageSync('userInfo').id,
				finished: false,
				pageNo: 1,
				list: []
			}
		},
		onLoad() {
			this.init();
		},
		filters: {
			formatStatus(status) {
				switch (status) {
					case 0:
						return '待付款';
					case 1:
						return '待发货';
					case 2:
						return '待收货';
					case 3:
						return '已完成';
					case 4:
						return '取消订单';
					default:
						return '';
				}
			}
		},
		methods: {
			init() {
				this.finished = false;
				this.pageNo = 1;
				this.list = [];
				this.getList();
			},
			getList() {
				this.$api.http('/user/userIntegralRecord/list', 'GET', {
					pageNo: this.pageNo++,
					pageSize: 20,
					type: 1,
					userId: this.userId
				}, res => {
					if (res.data.code == 200) {
						if (res.data.result.records.length < 20) {
							this.finished = true;
						}
						this.list = this.list.concat(res.data.result.records);
					} else {
						this.pageNo--;
						this.$Toast(res.data.message || '请求失败，请重试！');
					}
					uni.stopPullDownRefresh();
				});
			},
		},
		onPullDownRefresh() {
			this.init();
		},
		onReachBottom() {
			this.getList();
		}
	}
</script>

<style lang="less" scoped>
	.view {
		width: 100%;
		background-color: #F1F1F1;

		.list {
			.item {
				display: flex;
				flex-direction: row;
				align-items: center;
				margin-bottom: 1rpx;
				padding: 30rpx;
				background-color: #FFFFFF;

				.info {
					flex: 1;

					view {
						font-size: 24rpx;
						color: #999999;

						&:first-child {
							margin-bottom: 10rpx;
							color: #333333;
							font-size: 30rpx;
						}

						text {
							margin-left: 30rpx;
							color: #007AFF;
						}
					}
				}
				
				.integral {
					text-align: end;
					
					view {
						font-size: 24rpx;
						color: #999999;
						
						&:first-child {
							margin-bottom: 10rpx;
							font-size: 30rpx;
							color: #333333;
						}
					}
				}
			}
		}
	}
</style>
